package com.senjoeson.socialsecure.ui;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.view.MenuItem;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.google.gson.Gson;
import com.senjoeson.socialsecure.R;
import com.senjoeson.socialsecure.adapter.ListAdapter;
import com.senjoeson.socialsecure.model.ArticleListBean;
import com.senjoeson.socialsecure.model.CommentsBean;
import com.senjoeson.socialsecure.utils.ParseUtils;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

/**
 * @author senjoeson
 * @date 2018/9/17 0:40
 * @function 此类是所有列表的入口，会根据不同的类型显示数据结果
 */
public class ListActivity extends AppCompatActivity implements View.OnClickListener {
    private static final int UPDATE_UI = 0X01;
    @BindView(R.id.recyclerview)
    RecyclerView mRecyclerview;

    TextView mToolbarTitle;
    @BindView(R.id.toolbar)
    Toolbar mToolbar;
    @BindView(R.id.view_1)
    ImageView mView1;
    @BindView(R.id.view_2)
    ImageView mView2;
    @BindView(R.id.view_3)
    ImageView mView3;
    @BindView(R.id.view_layout)
    LinearLayout mViewLayout;
    private List<ArticleListBean> mArticleListBeans = new ArrayList<>();

    @SuppressLint("HandlerLeak")
    public Handler mHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case UPDATE_UI:
                    mListAdapter.notifyDataSetChanged();
                    break;
            }

        }
    };
    public ListAdapter mListAdapter;
    public Unbinder mUnbinder;
    public int loadDataType;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_list);
        mUnbinder = ButterKnife.bind(this);
        setToolBar();

        mRecyclerview.setLayoutManager(new LinearLayoutManager(this));
        mListAdapter = new ListAdapter(mArticleListBeans);
        mRecyclerview.setAdapter(mListAdapter);
        loadDataType = getIntent().getIntExtra("loadDataType", 0);
        boolean isService = getIntent().getBooleanExtra("isService", false);
        String title = getIntent().getStringExtra("title");
        mToolbarTitle.setText(title);
        if (isService) {
            mViewLayout.setVisibility(View.VISIBLE);
        } else {
            mViewLayout.setVisibility(View.GONE);
        }
        mView1.setOnClickListener(this);
        mView2.setOnClickListener(this);
        mView3.setOnClickListener(this);
        if (loadDataType > 1) {
            loadCommentsData();
        } else {
            loadData();
        }
    }

    /**
     * 设置toolbar属性
     */
    private void setToolBar() {
        setSupportActionBar(mToolbar);
        mToolbarTitle = mToolbar.findViewById(R.id.toolbar_title);
        if (getSupportActionBar() != null) {
            getSupportActionBar().setDisplayShowTitleEnabled(false);
            getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        }
    }

    /**
     * 加载数据
     */
    public void loadData() {
        final String url = getIntent().getStringExtra("url");
        if (url != null) {
            new Thread(new Runnable() {
                @Override
                public void run() {
                    List<ArticleListBean> articleListBeans = null;
                    if (loadDataType == 0) {
                        articleListBeans = ParseUtils.getArticles3Tab(url);
                    } else if (loadDataType == 1) {
                        articleListBeans = ParseUtils.parseData44Tab(url);
                    }
                    if (articleListBeans != null) {
                        mArticleListBeans.addAll(articleListBeans);
                    }

                    mHandler.sendEmptyMessage(UPDATE_UI);
                }
            }).start();
        }

    }

    /**
     * 此处是死数据
     */
    public void loadCommentsData() {
        InputStream is = null;
        try {
            is = getAssets().open("comment.json");
            int lenght = is.available();
            byte[] buffer = new byte[lenght];
            is.read(buffer);
            String result = new String(buffer, "utf8");
            CommentsBean commentsBean = new Gson().fromJson(result, CommentsBean.class);
            List<CommentsBean.RowsBean> rows = commentsBean.getRows();
            ArrayList<ArticleListBean> articleListBeans = new ArrayList<>();
            for (int i = 0; i < rows.size(); i++) {
                ArticleListBean articleListBean = new ArticleListBean();
                CommentsBean.RowsBean rowsBean = rows.get(i);
                articleListBean.setTitle(rowsBean.getTitle());
                if ("男".equals(rowsBean.getSex())) {
                    articleListBean.setTextFlag(rowsBean.getFname() + "先生");
                } else {
                    articleListBean.setTextFlag(rowsBean.getFname() + "女士");
                }
                articleListBean.setTime(rowsBean.getQuestiontime());
                articleListBean.setArticleUrl("http://new.sysb.gov.cn/mess/detail" + rowsBean.getUuid());
                articleListBeans.add(articleListBean);
            }
            mArticleListBeans.addAll(articleListBeans);
        } catch (IOException e) {
            e.printStackTrace();
        }


    }


    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home:
                onBackPressed();
                break;
        }
        return super.onOptionsItemSelected(item);
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (mUnbinder != null) {
            mUnbinder.unbind();
        }
    }

    @Override
    public void onClick(View v) {
        Intent intent = new Intent(ListActivity.this, WebviewActivity.class);
        switch (v.getId()) {
            case R.id.view_1:
                intent.putExtra("url", "http://new.sysb.gov.cn/pages/10/more_10.html");
                intent.putExtra("title", "教育实践活动");
                break;
            case R.id.view_2:
                intent.putExtra("url", "http://www.shenyang.gov.cn/zwgk/zwdt/rdyw/index.shtml");
                intent.putExtra("title", "国务院信息");
                break;
            case R.id.view_3:
                intent.putExtra("url", "http://new.sysb.gov.cn/pages/9/more_9.html");
                intent.putExtra("title", "养老党建工作");
                break;
        }
        startActivity(intent);
    }
}
